﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using z_storm.suny.F_ALL;

namespace z_storm.suny.F_ALL
{
    public class OPVcDll
    {
        //#region 对USB接口的使用(CPU卡)
        //[DllImport("dcrf32.dll")]
        //private static extern int dc_init(Int16 port, long baud);  //初试化
        //[DllImport("dcrf32.dll")]
        //private static extern short dc_exit(int icdev);//退出
        //[DllImport("dcrf32.dll")]
        //private static extern short dc_reset(int icdev, uint sec);
        //[DllImport("dcrf32.dll")]
        //private static extern short dc_request(int icdev, char _Mode, ref uint TagType);//检测卡
        //[DllImport("dcrf32.dll")]
        //private static extern short dc_card(int icdev, char _Mode, ref ulong Snr);
        //[DllImport("dcrf32.dll")]
        //private static extern short dc_pro_halt(int icdev);
        //[DllImport("dcrf32.dll")]
        //private static extern short dc_anticoll(int icdev, char _Bcnt, ref ulong IcCardNo);
        //[DllImport("dcrf32.dll")]
        //private static extern short dc_beep(int icdev, uint _Msec);////蜂鸣
        //[DllImport("dcrf32.dll")]
        //private static extern short dc_authentication(int icdev, int _Mode, int _SecNr);

        //[DllImport("dcrf32.dll")]
        //private static extern short dc_load_key(int icdev, int mode, int secnr, [In] byte[] nkey);  //密码装载到读写模块中
        //[DllImport("dcrf32.dll")]
        //private static extern short dc_load_key_hex(int icdev, int mode, int secnr, string nkey);  //密码装载到读写模块中

        //[DllImport("dcrf32.dll")]
        //private static extern short dc_write(int icdev, int adr, [In] byte[] sdata);  //向卡中写入数据
        //[DllImport("dcrf32.dll")]
        //private static extern short dc_write(int icdev, int adr, [In] string sdata);  //向卡中写入数据
        //[DllImport("dcrf32.dll")]
        //private static extern short dc_write_hex(int icdev, int adr, [In] string sdata);  //向卡中写入数据(转换为16进制)

        //[DllImport("dcrf32.dll")]
        //private static extern short dc_read(int icdev, int adr, [Out] byte[] sdata);

        //[DllImport("dcrf32.dll")]
        //private static extern short dc_read(int icdev, int adr, [MarshalAs(UnmanagedType.LPStr)] StringBuilder sdata);  //从卡中读数据
        //[DllImport("dcrf32.dll")]
        //private static extern short dc_read_hex(int icdev, int adr, [MarshalAs(UnmanagedType.LPStr)] StringBuilder sdata);  //从卡中读数据(转换为16进制)
        //[DllImport("dcrf32.dll")]
        //private static extern int a_hex(string oldValue, ref string newValue, Int16 len);  //普通字符转换成十六进制字符
        //[DllImport("dcrf32.dll")]
        //private static extern void hex_a(ref string oldValue, ref string newValue, int len);  //十六进制字符转换成普通字符
        ///// <summary>
        ///// 读取卡信息  (unsigned char * )
        ///// </summary>
        ///// <param name="icdev"></param>
        ///// <param name="_Snrlen"></param>
        ///// <returns></returns>
        //[DllImport("PdocMACDll.dll")]
        //private static extern int mif_CPU_Card(int icdev, [MarshalAs(UnmanagedType.LPArray)]byte[] _Snrlen);
        //private static int mif_CPU_Card(int icdev, ref byte[] _Snrlen)
        //{
        //    return mif_CPU_Card(icdev, _Snrlen);
        //}
        ///// <summary>
        ///// 读取卡内部信息
        ///// </summary>
        ///// <param name="icdev"></param>
        ///// <param name="a_dkey"></param>
        ///// <param name="a_len"></param>
        ///// <param name="_Snrlen"></param>
        ///// <param name="a_jud"></param>
        ///// <param name="a_snr"></param>
        ///// <returns></returns>
        //[DllImport("PdocMACDll.dll")]
        //private static extern int mif_CPU_Wr_RdInfo(int icdev, string a_dkey, int a_len, [MarshalAs(UnmanagedType.LPArray)]byte[] _Snrlen, int a_jud, string a_snr);
        //private static int mif_CPU_Wr_RdInfo(int icdev, string a_dkey, int a_len, ref byte[] _Snrlen, int a_jud, string a_snr)
        //{
        //    return mif_CPU_Wr_RdInfo(icdev, a_dkey, a_len, _Snrlen, a_jud, a_snr);
        //}

        //[DllImport("PdocMACDll.dll")]
        //private static extern int CloseComm(int icdev);
        ///// <summary>
        ///// 初始化设备
        ///// </summary>
        //private static int Icdev
        //{
        //    get
        //    {
        //        int dc_init = OPVcDll.dc_init(100, 115200);
        //        if (dc_init > 0)
        //        {
        //            return dc_init;
        //        }
        //        else
        //        {
        //            throw new Exception("初始化失败！");
        //        }

        //    }

        //}

        ///// <summary>
        ///// 获取卡信息  
        ///// </summary>
        ///// <returns></returns>
        //public static string ReadIc()
        //{
        //    string cardId = "";
        //    int st;
        //    byte[] icCardNo = new byte[32];
        //    char tt = (char)1;
        //    uint ss = 0;
        //    int dc_init = Icdev;
        //    dc_beep((Int16)dc_init, 50);
        //    dc_pro_halt(dc_init);    //释放
        //    st = dc_request(dc_init, tt, ref ss); //检测卡
        //    if (st != 0)
        //    {
        //        dc_exit(dc_init); //退出
        //        throw new CustomException("获取卡信息出错!");
        //    }
        //    //是否为CPU卡
        //    if (ss != 8)
        //    {
        //        dc_exit(dc_init); //退出
        //        throw new CustomException("该卡为非CPU卡不可以在本功能中使用!");
        //    }

        //    mif_CPU_Card(dc_init, ref icCardNo);
        //    string str = Encoding.Default.GetString(icCardNo).TrimEnd('\0');
        //    if (str != null)
        //    {
        //        cardId = str;
        //    }
        //    else
        //    {
        //        dc_exit(dc_init); //退出
        //        throw new CustomException("寻卡失败！");
        //    }
        //    dc_exit(dc_init);
        //    return cardId;
        //}
        ///// <summary>
        ///// 读取卡内信息
        ///// </summary>
        ///// <param name="cardId">卡号</param>
        ///// <returns></returns>
        //public static string readCardInfo(string cardId)
        //{
        //    byte[] icCardInfo = new byte[32];
        //    int LI_RET = mif_CPU_Wr_RdInfo(Icdev, OverAll.GS_DKEY, 20, ref icCardInfo, 1, cardId);
        //    string str = Encoding.Default.GetString(icCardInfo).TrimEnd('\0');
        //    return str;
        //}

        ///// <summary>
        ///// 写卡内信息
        ///// </summary>
        ///// <param name="cardId">卡号</param>
        ///// <param name="icCardInfo">写入卡的信息</param>
        //public static void WriteCardInfo(string cardId, string icCardInfo)
        //{
        //    if (cardId != ReadIc())
        //    {
        //        throw new CustomException("当前卡与检测卡不是同一张卡，请重新检测！");
        //    }

        //    byte[] ByteCardInfo = Encoding.Default.GetBytes(icCardInfo);
        //    int LI_RET = mif_CPU_Wr_RdInfo(Icdev, OverAll.GS_DKEY, 20, ByteCardInfo, 0, cardId);
        //    if (LI_RET != 0)
        //    {
        //        throw new CustomException("写卡失败!", ExceptionType.Warn);
        //    }
        //}

        //#endregion

        //#region 加密狗
        //[DllImport("Poo.dll")]
        //public static extern int f_gethd();
        //#endregion

        //打开链接
        [DllImport("z_hbxj.DLL")]
        public static extern int f_setupdatestart();

        //打开链接
        [DllImport("z_xywl.DLL")]
        public static extern int OpenComm(int a_port);
        //关闭连接
        [DllImport("z_xywl.DLL")]
        public static extern int CloseComm(int a_handle);
        //初始化卡
        [DllImport("z_xywl.DLL")]
        public static extern int Hw_Init_UserCard(IntPtr handle, int a_val1, int a_val2, int a_val3, ref int a_nkh, ref string a_nkhh);
        //回收卡
        [DllImport("z_xywl.DLL")]
        public static extern int Hw_Rst_UserCard(long handle, long a_val1, long a_val2, long a_val3, ref ulong a_nkh, ref string a_nkhh);
        //读卡
        [DllImport("z_xywl.DLL")]
        private static extern int Hw_Red_UserCard(int handle, ref int a_kh, ref int a_val1, ref int a_val2, ref int a_val3, ref int a_klb, ref int a_xkf, ref int a_nkh, [MarshalAs(UnmanagedType.LPArray)]byte[] a_nkhh);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="handle">句柄</param>
        /// <param name="a_kh">卡号</param>
        /// <param name="a_val1">金额1</param>
        /// <param name="a_val2">金额2</param>
        /// <param name="a_val3">金额3</param>
        /// <param name="a_klb">卡类别</param>
        /// <param name="a_xkf">是否新卡</param>
        /// <param name="a_nkh">数字内卡号</param>
        /// <param name="a_nkhh">内卡号</param>
        /// <returns></returns>
        public static int Hw_Red_UserCard(int handle, ref int a_kh, ref int a_val1, ref int a_val2, ref int a_val3, ref int a_klb, ref int a_xkf, ref int a_nkh, ref byte[] a_nkhh)
        {
            return Hw_Red_UserCard(handle, ref  a_kh, ref  a_val1, ref  a_val2, ref  a_val3, ref  a_klb, ref  a_xkf, ref  a_nkh, a_nkhh);
        }
        //写入金额
        [DllImport("z_xywl.DLL")]
        public static extern int Hw_Set_UserAmount1(long handle, long a_val1, ref ulong a_nkh, ref string a_nkhh);
        [DllImport("z_xywl.DLL")]
        private static extern int Hw_Set_UserAmount2(int handle, int a_val2, ref int a_nkh, [MarshalAs(UnmanagedType.LPArray)]byte[] a_nkhh);
        public static int Hw_Set_UserAmount2(int handle, int a_val2, ref int a_nkh, ref byte[] a_nkhh)
        {
            return Hw_Set_UserAmount2(handle, a_val2, ref a_nkh, a_nkhh);
        }
        [DllImport("z_xywl.DLL")]
        public static extern int Hw_Set_UserAmount3(long handle, long a_val3, ref ulong a_nkh, ref string a_nkhh);

        //读取狗的信息
        [DllImport("z_xywl.DLL")]
        private static extern int Hw_F_GetDog([MarshalAs(UnmanagedType.LPArray)]byte[] a_nkhh);
        public static int Hw_F_GetDog(ref byte[] a_nkhh)
        {
            return Hw_F_GetDog(a_nkhh);
        }
    }
}
